package chapter17;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ComboBoxDemo extends JFrame{
	
	private String[] flagTitles = {"Canada", "China", "Denmark", "France", "Germany", "India", "Norway", "United Kingdom", "United States of America"};
	
	private ImageIcon[] flagImage = {
			new ImageIcon(ClassLoader.getSystemResource("chapter17/image/canada.jpg")),
			new ImageIcon(ClassLoader.getSystemResource("chapter17/image/china.jpg")),
			new ImageIcon(ClassLoader.getSystemResource("chapter17/image/denmark.jpg")),
			new ImageIcon(ClassLoader.getSystemResource("chapter17/image/france.jpg")),
			new ImageIcon(ClassLoader.getSystemResource("chapter17/image/germany.jpg")),
			new ImageIcon(ClassLoader.getSystemResource("chapter17/image/india.jpg")),
			new ImageIcon(ClassLoader.getSystemResource("chapter17/image/norway.jpg")),
			new ImageIcon(ClassLoader.getSystemResource("chapter17/image/uk.jpg")),
			new ImageIcon(ClassLoader.getSystemResource("chapter17/image/us.jpg"))
	};
	
	private String[] flagDescription = new String[9];
	
	private DescriptionPanel descriptionPanel = new DescriptionPanel();
	
	private JComboBox jcbo = new JComboBox(flagTitles);

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ComboBoxDemo frame = new ComboBoxDemo();
		frame.setSize(500, 350);
		frame.setTitle("ComboBoxDemo");
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	
	public ComboBoxDemo(){
		flagDescription[0] = "The Maple Leaf flag\n\n" + 
				"The Canadian National Flag was adopted by the Canadian " +
				"Parliament on October 22, 1964 and was proclaimed into law " +
				"by Her Majesty Queen Elizaberth II (the Queen of Canada) on " +
				"February 15, 1965. The Canadian Flag (colloquially known " +
				"as The Maple Leaf Flag) is a red flag of the proportions " +
				"two by length and one by width, containing in its center a " +
				"white square, with a single red stylized eleven-point " +
				"maple leaf center in the white square.";
		flagDescription[1] = "Description for China...";
		flagDescription[2] = "Description for Denmark...";
		flagDescription[3] = "Description for France...";
		flagDescription[4] = "Description for Germany...";
		flagDescription[5] = "Description for India...";
		flagDescription[6] = "Description for Norway...";
		flagDescription[7] = "Description for UK...";
		flagDescription[8] = "Description for US...";
		
		setDisplay(0);
		
		add(jcbo, BorderLayout.NORTH);
		add(descriptionPanel, BorderLayout.CENTER);
		
		jcbo.addItemListener(new ItemListener(){
			public void itemStateChanged(ItemEvent e){
				setDisplay(jcbo.getSelectedIndex());
			}
		});
	}
	
	public void setDisplay(int index){
		descriptionPanel.setTitle(flagTitles[index]);
		descriptionPanel.setImageIcon(flagImage[index]);
		descriptionPanel.setDescription(flagDescription[index]);
	}

}
